package com.broadengate.action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.struts.actions.DispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.broadengate.form.OpenAccountForm;
import com.broadengate.model.Bank;
import com.broadengate.model.Card;
import com.broadengate.model.Customer;
import com.broadengate.service.AdminService;
import com.broadengate.service.BankService;
import com.broadengate.service.CardService;


public class OpenAccountAction extends DispatchAction {

	private AdminService adminService;
	private CardService cardService;
	private BankService bankService;
	
	/******
	 * 实现管理员开户
	 * 核心功能由调用AdminService对象的方法openAccount()；cardService对象方法的addCard()实现
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward 对象
	 */
	public ActionForward openAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		HttpSession session=request.getSession();
		System.out.println("openAccountAction 进入");
		float ammout = Float.parseFloat("0");
		OpenAccountForm openAccountForm=(OpenAccountForm)form;
		Customer customer=new Customer();
		Card newCard=new Card();
		Bank newBank=new Bank();
		//Set cardSet=new HashSet(0);
		if(cardService.findCardByCardNO(openAccountForm.getCardno())!=null)
		{
			 session.setAttribute("wrong","have");
		     return mapping.findForward("fail");
		}
			
		//cardSet.add();
		customer.setAddress(openAccountForm.getAddress());
		//System.out.println(openAccountForm.getYear()+ openAccountForm.getMonth() + openAccountForm.getDay());
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Date birthday = sdf.parse(openAccountForm.getYear() + openAccountForm.getMonth()+ openAccountForm.getDay() );
            customer.setBirthday(birthday);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		customer.setEmail(openAccountForm.getEmail());
		customer.setGender(openAccountForm.getGender());
		//customer.setCards(cardSet);
		customer.setIdentityId(openAccountForm.getIdentityId());
		customer.setMobile(openAccountForm.getMobile());
		customer.setName(openAccountForm.getName());
		customer.setNativeplace(openAccountForm.getNativeplace());
		customer.setTel(openAccountForm.getPhone());
		
		newCard.setCardNo(openAccountForm.getCardno());
		newCard.setCustomer(customer);
		newCard.setDollarBalance(ammout);
		newCard.setEuroBalance(ammout);
		newCard.setPassword(openAccountForm.getPassword());
		newCard.setRmbbalance(ammout);
		newCard.setBank(bankService.findBankByBankNO("3"));
		try {
			 adminService.openAccount(customer);
				cardService.addCard(newCard);
				return mapping.findForward("success");
			

		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("fail");
		}

	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public CardService getCardService() {
		return cardService;
	}

	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}

	public BankService getBankService() {
		return bankService;
	}

	public void setBankService(BankService bankService) {
		this.bankService = bankService;
	}
	
}
